package com.stations.common.utils;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author Geordie
 * @version 0.0.1
 * @description StringUtill
 * @since 2023/4/3 17:47
 */
public class StringUtils {
    private static final Log log = LogFactory.get(StringUtils.class);

    /**
     * 对象转json字符串
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> String ObjectToString(T t) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String objStr = objectMapper.writeValueAsString(t);
            return objStr;
        } catch (JsonProcessingException e) {
            log.error("对象转json字符串失败，对象：{}，失败原因：{}", t, e.getMessage());
            return null;
        }
    }
}
